#ifndef __GS_TANK_H__
#define __GS_TANK_H__

#include <pthread.h>

#define ALARM_O_UUPPER      0x00000001
#define ALARM_O_UPPER       0x00000002
#define ALARM_O_LOWER       0x00000004
#define ALARM_O_LLOWER      0x00000008
#define ALARM_O_MASK        0x0000000F

#define ALARM_W_UPPER       0x00000010
#define ALARM_W_LOWER       0x00000020
#define ALARM_W_MASK        0x000000F0

#define ALARM_T_UPPER       0x00000100
#define ALARM_T_LOWER       0x00000200
#define ALARM_T_MASK        0x00000F00



typedef struct 
{
    pthread_mutex_t lock;
    int tank_id;
    int height;
    
    /* oil infos */
    int oil_id;
    float density;

    /* sensor infos */
    int addr;
    int oil_b;
    int water_b;

    char table[128];

    int o_uupper;
    int o_upper;
    int o_lower;
    int o_llower;
    int w_upper;
    int w_lower;
    int t_upper;
    int t_lower;

    double oil;
    double water;
    double temp;
    double t1, t2, t3, t4, t5;

	double o_cache[4];
	double w_cache[4];
	double t_cache[4];
	int cache;
	
	int state; // -1 -uninit, 0 - stop, 1 - up, 2 - down, 3 -- leak
	time_t begin_time, end_time;
	double begin_oil, end_oil;

}GS_TANK;

typedef void (* GS_TANKCALLBACK) (GS_TANK *tank);

GS_TANKCALLBACK gs_tank_data_callback;
GS_TANKCALLBACK gs_tank_alarm_callback;

int gs_tank_is_wrong(GS_TANK* tank);
int gs_tank_is_alarm(GS_TANK* tank);
double gs_tank_get_volume(GS_TANK* tank, double oil);
long gs_tank_get_alarm(GS_TANK* tank);
int gs_tank_get_addr(GS_TANK* tank, unsigned char* addr);
GS_TANK* gs_tank_by_addr(unsigned char addr);
int gs_tank_data(GS_TANK *tank);
#endif
